﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>调试递归</title>
    <style>
        * { margin: 0; padding: 0;}
    </style>
</head>
<body>
    <div class="drag-box">
        <h2>我是标题</h2>
        <div class="content">
            内容区域
        </div>
    </div>
    <script>
        let resData = {"code":"0","isSuccess":true,"message":"ok!","errorCode":"0","data":{"treeType":"0","treeData":{"nodeId":"3884","nodeName":"高力物业","parentId":"0","nodeType":"1","layer":"0","authFlag":false,"childList":[{"nodeId":"3886","nodeName":"常州分公司","parentId":"3884","nodeType":"3","layer":"1","authFlag":false,"childList":[{"nodeId":"3962","nodeName":"常州字母项目部","parentId":"3886","nodeType":"4","layer":"2","authFlag":false,"childList":[{"nodeId":"445357915730083840","nodeName":"常州A项目","parentId":"3962","nodeType":"5","layer":null,"authFlag":false,"childList":[]}]},{"nodeId":"3902","nodeName":"常州汽配城服务中心","parentId":"3886","nodeType":"4","layer":"2","authFlag":false,"childList":[{"nodeId":"422348265879506944","nodeName":"常州汽配城","parentId":"3902","nodeType":"5","layer":null,"authFlag":false,"childList":[]}]}]},{"nodeId":"3887","nodeName":"扬州分公司","parentId":"3884","nodeType":"3","layer":"1","authFlag":false,"childList":[{"nodeId":"3910","nodeName":"扬州汽配城服务中心","parentId":"3887","nodeType":"4","layer":"2","authFlag":false,"childList":[{"nodeId":"426389306123227136","nodeName":"扬州汽配城","parentId":"3910","nodeType":"5","layer":null,"authFlag":false,"childList":[]}]},{"nodeId":"3913","nodeName":"扬州家居港服务中心","parentId":"3887","nodeType":"4","layer":"2","authFlag":false,"childList":[{"nodeId":"423849444853682176","nodeName":"扬州家居港","parentId":"3913","nodeType":"5","layer":null,"authFlag":false,"childList":[]}]}]},{"nodeId":"3888","nodeName":"无锡分公司","parentId":"3884","nodeType":"3","layer":"1","authFlag":false,"childList":[{"nodeId":"3895","nodeName":"无锡汽配城服务中心","parentId":"3888","nodeType":"4","layer":"2","authFlag":false,"childList":[{"nodeId":"423857741933580288","nodeName":"无锡汽配城","parentId":"3895","nodeType":"5","layer":null,"authFlag":false,"childList":[]}]}]},{"nodeId":"3889","nodeName":"盐城分公司","parentId":"3884","nodeType":"3","layer":"1","authFlag":false,"childList":[{"nodeId":"3909","nodeName":"盐城金色果缘服务中心","parentId":"3889","nodeType":"4","layer":"2","authFlag":false,"childList":[{"nodeId":"426354255696236544","nodeName":"金色果缘","parentId":"3909","nodeType":"5","layer":null,"authFlag":false,"childList":[]}]},{"nodeId":"3912","nodeName":"盐城家居港服务中心","parentId":"3889","nodeType":"4","layer":"2","authFlag":false,"childList":[{"nodeId":"432567135370215424","nodeName":"盐城家居港","parentId":"3912","nodeType":"5","layer":null,"authFlag":false,"childList":[]}]}]},{"nodeId":"3890","nodeName":"长春分公司","parentId":"3884","nodeType":"3","layer":"1","authFlag":false,"childList":[{"nodeId":"3911","nodeName":"长春汽贸城服务中心","parentId":"3890","nodeType":"4","layer":"2","authFlag":false,"childList":[{"nodeId":"425955278068912128","nodeName":"长春汽配城","parentId":"3911","nodeType":"5","layer":null,"authFlag":false,"childList":[]}]}]}]},"description":"描述：0全量树,1节点树"}};
        /*
		let loopFn = (itemObj) => {
            debugger
            let tempObj = {
                id: itemObj.nodeId,
                label: itemObj.nodeName
            }
            if (itemObj.nodeType === '5') {
                return tempObj;
            } else {
                let childList = itemObj.childList || []
                tempObj.children = [];
                childList.forEach(child => {
                    tempObj.children.push(loopFn(child))
                })
            }
            return tempObj;
        }
        if (resData.code === '0') {
            let result = loopFn(resData.data.treeData);
            console.dir(result);
            // this.orgOptions = [result];
        }
		*/
		
		let _this = this;
		_this.curOrg = '426354255696236544';
		let loopFn = (itemObj) => { // 递归获取选中的组织节点的数据信息
			if (!itemObj) return null;
			let obj = null;
			if (itemObj.nodeType === '5' && itemObj.nodeId === _this.curOrg) {                        
				return itemObj;
			} else if (itemObj.nodeType === '5' && itemObj.nodeId !== _this.curOrg) {
				return null;
			} else {
				let childList = itemObj.childList || [];
				for (let i = 0; i < itemObj.childList.length; i++) {
					let child = itemObj.childList[i];
					obj = loopFn(child);
					if (obj) break;
				}
			}
			return obj;
		}
		let currentNode = loopFn(resData.data.treeData);
		console.log(currentNode);
    </script>
</body>
</html>